package performance

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/disk"
	"github.com/shirou/gopsutil/host"
	"github.com/shirou/gopsutil/mem"
	"github.com/shirou/gopsutil/net"
	"runtime"
	"time"
)

// 定义控制器的结构体
type Controller struct{}

// 实例化方法
func New() *Controller {
	return &Controller{}
}

func printMemStats() {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	fmt.Printf("Alloc = %v TotalAlloc = %v Sys = %v NumGC = %v\n",
		m.Alloc/1024, m.TotalAlloc/1024, m.Sys/1024, m.NumGC)
}
func (c *Controller) Get(req *ghttp.Request) {
	memInfo, err := mem.VirtualMemory()
	if err != nil {
		fmt.Println("get memory info fail. err： ", err)
	}
	// 获取总内存大小，单位GB
	memTotal := memInfo.Total / 1024 / 1024 / 1024
	// 获取已用内存大小，单位MB
	memUsed := memInfo.Used / 1024 / 1024
	// 可用内存大小
	memAva := memInfo.Available / 1024 / 1024
	// 内存可用率
	memUsedPercent := memInfo.UsedPercent

	memMap := make(map[string]interface{})
	memMap["total"] = memTotal
	memMap["used"] = memUsed
	memMap["free"] = memAva
	memMap["usedPercent"] = memUsedPercent

	fmt.Printf("总内存: %v GB, 已用内存: %v MB, 可用内存: %v MB, 内存使用率: %.3f %% \n", memTotal, memUsed, memAva, memUsedPercent)
	req.Response.WriteTpl("performance/index.html", g.Map{})

}

func (c *Controller) Host(req *ghttp.Request) {
	statMap := make(map[string]interface{})
	// 获取主机相关信息
	hostInfo, _ := host.Info()
	hostMap := make(map[string]interface{})
	hostMap["uptime"] = hostInfo.Uptime                   //运行时间
	hostMap["bootTime"] = hostInfo.BootTime               //启动时间
	hostMap["procs"] = hostInfo.Procs                     //进程数
	hostMap["os"] = hostInfo.OS                           //操作系统
	hostMap["platform"] = hostInfo.Platform               //平台
	hostMap["platformVersion"] = hostInfo.PlatformVersion //平台版本
	hostMap["kernelArch"] = hostInfo.KernelArch           //内核
	hostMap["kernelVersion"] = hostInfo.KernelVersion     //内核版本
	statMap["hosts"] = hostMap

	// 获取内存信息
	memInfo, _ := mem.VirtualMemory()
	memMap := make(map[string]interface{})
	memMap["total"] = memInfo.Total / 1024 / 1024 / 1024         //总内存
	memMap["available"] = memInfo.Available / 1024 / 1024 / 1024 //可用内存
	memMap["used"] = memInfo.Used / 1024 / 1024 / 1024           //已使用内存
	memMap["free"] = memInfo.Free / 1024 / 1024 / 1024           //剩余内存
	memMap["usedPercent"] = memInfo.UsedPercent                  //百分比
	memMap["buffers"] = memInfo.Buffers                          //缓存
	memMap["shared"] = memInfo.Shared                            //共享内存
	memMap["cached"] = memInfo.Cached                            //缓冲区
	statMap["mems"] = memMap

	// 获取CPU信息
	cpuInfo, _ := cpu.Info()
	var cpuMapArr []map[string]interface{}
	for _, c := range cpuInfo {
		cpuMap := make(map[string]interface{})
		cpuMap["cpu"] = c.CPU + 1         //第几个CPU 从0开始的
		cpuMap["cores"] = c.Cores         //CPU的核数
		cpuMap["modelName"] = c.ModelName //CPU类型
		cpuMapArr = append(cpuMapArr, cpuMap)
	}
	percentages, err := cpu.Percent(time.Second, false)
	if err != nil {
		panic(err)
	}

	// 打印CPU使用率
	for _, percent := range percentages {
		fmt.Printf("CPU Usage: %.2f%%\n", percent)
		statMap["cpu"] = percent
	}
	statMap["cpus"] = cpuMapArr

	// 获取IO信息
	ioInfo, _ := net.IOCounters(false)
	var ioMapArr []map[string]interface{}
	for _, i := range ioInfo {
		ioMap := make(map[string]interface{})
		ioMap["ioName"] = i.Name             //网口名
		ioMap["bytesSent"] = i.BytesSent     //发送字节数
		ioMap["bytesRecv"] = i.BytesRecv     //接收字节数
		ioMap["packetsSent"] = i.PacketsSent //发送的数据包数
		ioMap["packetsRecv"] = i.PacketsRecv //接收的数据包数
		ioMapArr = append(ioMapArr, ioMap)
	}
	statMap["ios"] = ioMapArr

	// 获取磁盘信息
	partitions, _ := disk.Partitions(false)
	var diskMapArr []map[string]interface{}
	for _, partition := range partitions {
		diskMap := make(map[string]interface{})
		usage, _ := disk.Usage(partition.Mountpoint)
		diskMap["disk"] = partition.Mountpoint     //第几块磁盘
		diskMap["total"] = usage.Total             //总大小
		diskMap["free"] = usage.Free               //剩余空间
		diskMap["used"] = usage.Used               //已使用空间
		diskMap["usedPercent"] = usage.UsedPercent //百分比
		diskMapArr = append(diskMapArr, diskMap)
	}
	statMap["disks"] = diskMapArr
	req.Response.WriteJson(statMap)
}
